package wms.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import wms.entity.DataDict;
import wms.entity.Operator;
import wms.util.constant.SysConstant;

@Controller
public class GoodsTypeController extends BaseController{

	@RequestMapping("/base/typelist.html")
	public String getGoodsCodePage(Model model,
			@RequestParam(value="pageIndex",required=false) String curIndex, @RequestParam(value="pageSize",required=false) String pageSize) throws Exception{
		log.debug("物资类型管理页面------>");
		
		List<DataDict> typelist = null;
		
		//分页查询
		Integer currentIndex = null;
		Integer pageSizes = null;
		if (Objects.isNull(curIndex) || ("").equals(curIndex)) {
			currentIndex = SysConstant.DEFAULT_CURRNET_INDEX;
		} else {
			currentIndex = Integer.valueOf(curIndex);
		}
		
		if(Objects.isNull(pageSize) || ("").equals(pageSize)){
			pageSizes = SysConstant.DEFAULT_PAGESIZE;
		} else {
			pageSizes = Integer.valueOf(pageSize);
		}
		Page<DataDict> pages = PageHelper.startPage(currentIndex, pageSizes);	
		typelist = getDataDictService().listDataDict(SysConstant.DICT_GOODS_TYPE);
		
		model.addAttribute("pages", pages);
		model.addAttribute(SysConstant.DICT_GOODS_TYPE_LIST, typelist);
		
		return "goods/typelist";
	}
	
	
	@RequestMapping("/basic/type/modify")
	@ResponseBody
	public Map<String, Object> getGoodsTypeModifyData(@RequestParam("typeCode")String typeCode, @RequestParam("valueId") String valueId) throws Exception {
		log.debug("修改物资类型------->" + typeCode);
		Map<String, Object> map = new HashMap<String, Object>();
		if (typeCode == null || typeCode.equals("") || valueId == null || valueId.endsWith(""))
			map.put(SysConstant.MSG, SysConstant.FAIL);
		Integer valueid = Integer.valueOf(valueId);
		DataDict type = getDataDictService().getDataDict(typeCode, valueid);
		if (type != null) {
			map.put(SysConstant.MSG, type);
		}
		return map;
	}
	
	
	@RequestMapping("/basic/type/savemodify")
	public String saveGoodsTypeModify(DataDict dataDict, HttpSession session) throws Exception {
		log.debug("保存物资修改类型------------>");
		Operator operator = (Operator) session.getAttribute(SysConstant.OPERATOR);
		if (operator == null) {
			return "redirect:/";
		}
		dataDict.setModifyBy(operator.getLoginCode());
		dataDict.setModifyDate(new Date(System.currentTimeMillis()));
		if (getDataDictService().modifyDataDict(dataDict)){
			return "redirect:/base/typelist.html";
		}
		return "redirect:/";
	}
	
	
	
	@RequestMapping(value = "/basic/type/saveadd", method = RequestMethod.POST)
	public String saveRepo(DataDict dataDict, HttpSession session) throws Exception {
		log.debug("新增类别信息:------>类型"  + dataDict.getTypeCode() + "---取值:" + dataDict.getValueId());
		Operator operator = (Operator) session.getAttribute(SysConstant.OPERATOR);
		if (operator == null)
			return "redirect:/";
		
		dataDict.setCreateBy(operator.getLoginCode());
		dataDict.setCreateDate(new Date(System.currentTimeMillis()));
		
		if(getDataDictService().saveDataDict(dataDict)){
			return "redirect:/base/typelist.html";
		}
		return "redirect:/";
		
	}
	
	
}
